package com.wanghua.mall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wanghua.mall.ware.vo.FinishPurchaseVo;
import com.wanghua.mall.ware.vo.MergePurchaseDetailVo;
import org.springframework.web.bind.annotation.*;

import com.wanghua.mall.ware.entity.PurchaseEntity;
import com.wanghua.mall.ware.service.PurchaseService;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.R;

import javax.annotation.Resource;


/**
 * 采购信息
 *
 * @author wanghua
 * @email 2367279431@qq.com
 * @date 2021-08-25 21:29:07
 */
@RestController
@RequestMapping("/ware/purchase")
public class PurchaseController {

    @Resource
    private PurchaseService purchaseService;

    //完成采购
    @PostMapping("/done")
    public R donePurchase(@RequestBody FinishPurchaseVo finishPurchaseVo) {
        purchaseService.donePurchase(finishPurchaseVo);
        return R.ok();
    }

    //领取采购单
    @PostMapping("/received")
    public R receivePurchase(@RequestBody List<Long> purchaseIds) {
        purchaseService.receivePurchase(purchaseIds);
        return R.ok();
    }


    //合并采购需求
    @PostMapping("/merge")
    public R mergePurchaseDetail(@RequestBody MergePurchaseDetailVo mergePurchaseDetailVo) {
        purchaseService.mergePurchaseDetail(mergePurchaseDetailVo);
        return R.ok();
    }

    /**
     * 查询未领取的采购单
     */
    @GetMapping("/unreceive/list")
    public R listUnReceive(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.listUnReceive(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);
        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchaseService.save(purchase);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
